<script setup>
import {ElMessage, ElMessageBox} from "element-plus";
import store from "@/store";
import {onMounted, ref} from "vue";
import {getInfo} from "@/api/system/user";

const listItem = [
  {id: 1, name: "蛙币充值",},
  {id: 2, name: "修改密码",},
  {id: 3, name: "退出账号",},
];
const userInfo =ref({
  user :{},
  avatar: '',
})
onMounted(() => {
  getUserInfo()
})
const getUserInfo = () => {
  getInfo().then(res => {
    const user = res.user

    const avatar = (user.avatar === "" || user.avatar == null) ? require("@/assets/imgs/avatar.jpg") : process.env.BASE_URL + user.avatar;
    userInfo.value.user=user;
    userInfo.value.avatar=avatar;
  }).catch(error => {
    ElMessage.error("获取用户信息异常")
  })
}
const logout = async () => {
  ElMessageBox.confirm('是否退出账号？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    store.dispatch('LogOut').then(() => {
      location.href = '/index';
    })
    ElMessage.success("账号已退出!")
  }).catch(() => {
  });
}
const handleSelectionChange = (selection) => {
  if (!selection) return;
  switch (selection.id) {
    case 1:
      break;
    case 2:
      break;
    case 3:
      logout();
      break;
    default:
      break;  // 根据需要添加更多案例。例如，您可以处理其他用户操作。
  }
}
</script>

<template>
  <div class="user-info-box">
    <div class="info-header">
      <el-avatar style="background-color: transparent;" :size="50" :src="userInfo.avatar"/>
      <span>{{ userInfo.user.username }}</span>
      <div class="flex gap-2 time-box">
        <el-tag type="warning">蛙币：{{ userInfo.user.amount }}</el-tag>
        <el-tag type="danger">有效期：- -</el-tag>
      </div>

    </div>
    <el-table @row-click="handleSelectionChange" style="width: 100%" :data="listItem">
      <el-table-column align="center" prop="name" style="width: 100%;"/>
    </el-table>
  </div>
</template>

<style>
.info-header {
  padding: 10px;
  display: flex;
  width: 100%;
  justify-content: space-around;
  justify-items: center;
  align-items: center;
  flex-direction: column;
  color: white;
  height: 150px;
  background-color: #1482f0;
}

.time-box {
  width: 100%;
  display: flex;
  justify-content: space-around;
}

.el-tabs--border-card > .el-tabs__content {
  padding: 0 !important;
}
</style>